import { setDriverApi } from "@/api/waybillManagement/outWarehouseReceipt/index";
import { message } from "ant-design-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { formState, formRef, emit, passData } = state;

  const onSubmit = () => {
    formRef.value.validate().then(() => {
      const params = {
        ...formState.value,
        id: Array.isArray(passData.value) ? undefined : passData.value.id,
        idList: Array.isArray(passData.value) ? passData.value.map((t: any) => t.id) : undefined
      };

      setDriverApi(params).then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      });
    });
  };

  const resetForm = () => {
    formRef.value.resetFields();
    emit("update:visible", false);
  };

  const changeDriver = (e: string, option: any) => {
    formState.value.userDriverName = option.label;
  };

  const changeAddress = (e: string, option: any) => {
    formState.value.brokerAddress = option.label;
  };
  return {
    onSubmit,
    resetForm,
    changeDriver,
    changeAddress
  };
}
